/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package libraryproject;

import database_objects.Author;
import database_objects.Book;
import database_objects.Department;
import database_objects.EditionBook;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JDialog;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;
import user_client.LibrarianUserClient;

/**
 *
 * @author pawel
 */
public class EditionChoicePane extends javax.swing.JPanel {

    /**
     * Creates new form EditionChoicePane
     */
    public EditionChoicePane() {
        System.out.println("without");
        initComponents();
        this.model = (DefaultTableModel) resultTable.getModel();
        this.resultTable.getSelectionModel().addListSelectionListener(
                new MySelectionListener());
    }

    public EditionChoicePane(LibrarianUserClient client, Book currentBook,
            BooksManagementPane booksManagementsPane, JDialog owner,
            EditionBook edition, Department department) throws ParseException {
        System.out.println("with");
        this.client = client;

        initComponents();
        this.model = (DefaultTableModel) resultTable.getModel();
        authorsListModel = new DefaultListModel();
        authorsList.setModel(authorsListModel);
        this.resultTable.getSelectionModel().addListSelectionListener(
                new MySelectionListener());
        this.booksManagementsPane = booksManagementsPane;
        this.owner = owner;
        this.currentEdition = edition;
        this.titleField.setText(currentEdition.getTitle());
        this.jFormattedISBNField.setText(currentEdition.getIsbn());
        this.editionNameTextField.setText(
                currentEdition.getNameOfEditionHouse());
        this.dateOfEditionTextField.setText(
                currentEdition.getDateOfPublishing());
        this.currentDepartment = department;

        MaskFormatter formatter = new MaskFormatter("#############");
        DefaultFormatterFactory factory = new DefaultFormatterFactory(formatter);
        jFormattedISBNField.setFormatterFactory(factory);


    }

    public void setAuthors(ArrayList<Author> authors) {
        this.authors.clear();
        for (Author author : authors) {
            System.out.println(author.toString());
            authorsListModel.addElement(author);
            this.authors.add(author);
        }
        System.out.println("aktualizacja");
        jButton2.setEnabled(false);
    }

    public void setDepartment(Department department) {
        this.caseNumberLabel.setText(department.getBookCaseNumber());
        this.nameOfDepartment.setText(department.getName());
        this.currentDepartment = department;
        this.idOfDepartment = department.getId();
        jButton2.setEnabled(false);
    }

    private class MySelectionListener implements ListSelectionListener {

        @Override
        public void valueChanged(ListSelectionEvent e) {
            // TODO Auto-generated method stub


            try {
                isbn = (String) model.getValueAt(resultTable.getSelectedRow(), 0);

                Book book = null;
                try {
                    book = client.getEditionInfo(isbn, null, null);
                } catch (SQLException ex) {
                    Logger.getLogger(BooksManagementPane.class.getName()).log(Level.SEVERE, null, ex);
                }

                nameOfDepartment.setText(book.getNameOfDepartment());
                caseNumberLabel.setText(book.getBookCaseNumber());

                editionNameTextField.setText(book.getNameOfEdition());
                dateOfEditionTextField.setText(book.getDateOfEdition());
                jFormattedISBNField.setText(book.getIsbn());
                titleField.setText(book.getTitle());

                //positionFormatedField.setText(currentBook.getPositionOnBookCase());
                //fthis.authorsListModel.toArray();
                authorsListModel.clear();
                authors.clear();
                for (Author author : book.getAuthors()) {
                    authorsListModel.addElement(author);
                    authors.add(author);
                }

                title = (String) model.getValueAt(resultTable.getSelectedRow(),
                        1);
                nameOfEditionHouse = (String) model.getValueAt(
                        resultTable.getSelectedRow(), 2);

                String dateOfPublishingStr = (String) model.getValueAt(resultTable.getSelectedRow(), 3);
                idOfDepartment = (String) model.getValueAt(resultTable.getSelectedRow(), 4);

                System.out.println("IDofDepartment");
                System.out.println(idOfDepartment);
                System.out.println(idOfDepartment + ", " + title + ", "
                        + nameOfEditionHouse);

                resultLabel.setText(isbn + " " + title + " "
                        + nameOfEditionHouse);
                jButton2.setEnabled(true);
            } catch (ArrayIndexOutOfBoundsException e1) {
                resultLabel.setText("nie wybrano działu");
                idOfDepartment = null;
                nameOfEditionHouse = "";
                isbn = null;

            }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel7 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        resultTable = new javax.swing.JTable();
        resultLabel = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        editionNameTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        dateOfEditionTextField = new javax.swing.JFormattedTextField();
        titleField = new javax.swing.JTextField();
        jFormattedISBNField = new javax.swing.JFormattedTextField();
        jButton5 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jPanel5 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        authorsList = new javax.swing.JList();
        jButton3 = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        nameOfDepartment = new javax.swing.JLabel();
        caseNumberLabel = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        searchButton = new javax.swing.JButton();
        removeButton = new javax.swing.JButton();
        addButton = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        resultTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "isbn", "tytuł", "wydawnictwo", "data wydania", "id działu"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(resultTable);

        resultLabel.setText("Nie wybrano żadnego działu.");

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel7Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 565, Short.MAX_VALUE)
                    .addComponent(resultLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(20, 20, 20))
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(resultLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel4.setText("tytuł:");

        jLabel1.setText("isbn:");

        editionNameTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                editionNameTextFieldKeyTyped(evt);
            }
        });

        jLabel2.setText("wydawnictwo:");

        jLabel3.setText("data wydania:");

        dateOfEditionTextField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter()));
        dateOfEditionTextField.setText("YYYY-MM-DD");
        dateOfEditionTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dateOfEditionTextFieldActionPerformed(evt);
            }
        });
        dateOfEditionTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                dateOfEditionTextFieldKeyTyped(evt);
            }
        });

        titleField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                titleFieldKeyTyped(evt);
            }
        });

        jFormattedISBNField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jFormattedISBNFieldKeyTyped(evt);
            }
        });

        jButton5.setText("clear");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton4.setText("clear");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jFormattedISBNField, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(editionNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(titleField, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(dateOfEditionTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addGap(6, 6, 6))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(titleField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(editionNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jFormattedISBNField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(dateOfEditionTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton5))
                .addGap(40, 40, 40))
        );

        jPanel2.add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(12, 12, -1, 241));

        jScrollPane2.setViewportView(authorsList);

        jButton3.setText("wybierz autorów");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap(15, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton3)
                .addGap(33, 33, 33))
        );

        jPanel2.add(jPanel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 12, -1, 179));

        jLabel5.setText("numer regału:");

        jLabel6.setText("nazwa działu");

        nameOfDepartment.setText("nie wybrano działu");

        caseNumberLabel.setText("nie określono regału");

        jButton1.setText("wybierz dział");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton6.setText("wyczyść inf. o dziale");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(nameOfDepartment)
                            .addComponent(jLabel5)
                            .addComponent(caseNumberLabel))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jButton6, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(nameOfDepartment, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(caseNumberLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton6))
        );

        jPanel2.add(jPanel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(400, 12, -1, -1));

        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        searchButton.setText("szukaj");
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButtonActionPerformed(evt);
            }
        });
        jPanel1.add(searchButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 0, 110, -1));

        removeButton.setText("usuń");
        removeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeButtonActionPerformed(evt);
            }
        });
        jPanel1.add(removeButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 0, 100, -1));

        addButton.setText("dodaj");
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });
        jPanel1.add(addButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 0, 100, -1));

        jPanel2.add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 197, 321, -1));

        jButton2.setText("zatwierdź wybór");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(242, 232, 309, -1));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, 584, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 584, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(42, Short.MAX_VALUE)
                .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, 276, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void dateOfEditionTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dateOfEditionTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_dateOfEditionTextFieldActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        try {
            if (idOfDepartment != null) {
                EditionBook edition = new EditionBook(/*
                         * isbn, nameOfEditionHouse, dateOfPublishing, title, idOfDepartment
                         */
                        jFormattedISBNField.getText(), editionNameTextField.getText(),
                        dateOfEditionTextField.getText(), titleField.getText(),
                        idOfDepartment);

                this.currentEdition = edition;
                Department department = client.getDepartment(Integer.valueOf(idOfDepartment));

                // booksManagementsPane.setEdition(edition, department);
                booksManagementsPane.setBook(edition, department, authors);

            }
            if (owner != null) {
                owner.dispose();
            }

            // TODO add your handling code here:
        } catch (SQLException ex) {
            Logger.getLogger(EditionChoicePane.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
        String name = editionNameTextField.getText();
        // String numberOfPane = dataPane.
        String isbnText = jFormattedISBNField.getText();
        String dateOfEditionText = dateOfEditionTextField.getText();
        String title = titleField.getText();
        String idOfDep;
        Object[] authorsObj = authorsListModel.toArray();
        this.authors.clear();
        for (Object obj : authorsObj) {
            this.authors.add((Author) obj);
        }

        if (idOfDepartment != null) {
            idOfDep = idOfDepartment.toString();
        } else {
            idOfDep = "";
        }

        System.out.println(isbnText.length());
        System.out.println(dateOfEditionText.length());
        System.out.println(title.length());
        System.out.println(idOfDepartment);
        System.out.println(name);
        if ((name.length() > 0)
                && (isbnText.length() > 0)
                && (dateOfEditionText.length() > 0)
                && (title.length() > 0) && (idOfDep.length() > 0)) {

            try {
                Integer index = client.addEdition(isbnText,
                        name, dateOfEditionText, title, idOfDep, this.authors);
                if (index != -1) {
                    System.out.println("INDEX:" + index);
                    String[] newRow = new String[5];
                    newRow[0] = isbnText;
                    newRow[1] = title;
                    newRow[2] = name;
                    newRow[3] = dateOfEditionText;
                    newRow[4] = idOfDep;
                    model.addRow(newRow);
                    model.fireTableDataChanged();
                    resultTable.revalidate();
                    resultTable.repaint();

                    resultLabel.setText("Dodano element");
                } else {
                    resultLabel.setText("Podane wydanie już istnieje");
                }
            } catch (SQLException e1) {
                resultLabel.setText("Podane wydanie już istnieje");
            }

        } else {
            System.out.println("nie spełniony warunek");
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_addButtonActionPerformed

    private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed
        try {

            String idOfDepartmentString;
            if (idOfDepartment != null) {
                idOfDepartmentString = idOfDepartment;
            } else {
                idOfDepartmentString = "";
            }
            //System.out.println(idOfDepartment.toString());
            System.out.println("1");
            ArrayList<EditionBook> editions = client.searchEditions(
                    jFormattedISBNField.getText(),
                    editionNameTextField.getText(),
                    dateOfEditionTextField.getText(),
                    titleField.getText(),
                    idOfDepartmentString);

            System.out.println("2");

            while (model.getRowCount() > 0) {
                try {
                    model.removeRow(0);
                } catch (Exception a) {
                    System.out.println("ERRRORRRRRRRR????");
                }

            }


            System.out.println("SIZE: " + editions.size());
            for (EditionBook edition : editions) {
                System.out.println(edition.getNameOfEditionHouse());
                String[] val = new String[5];
                val[0] = edition.getIsbn();
                val[1] = edition.getTitle();
                val[2] = edition.getNameOfEditionHouse();
                val[3] = edition.getDateOfPublishing();
                val[4] = edition.getIdOfDepartment();
                model.addRow(val);
            }

            resultTable.revalidate();

            resultTable.repaint();

        } catch (SQLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }        // TODO add your handling code here:
    }//GEN-LAST:event_searchButtonActionPerformed

    private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
        try {
            if (isbn != null) {
                client.removeElement(isbn.toString(), "wydania",
                        "isbn");
            }
            int i = 0;
            // Integer size =
            // DepartmentManagerPane.this.model.getRowCount();

            while (i < model.getRowCount()) {
                String valS = ((String) resultTable.getValueAt(
                        i, 0));

                if (valS.equals(isbn)) {
                    model.removeRow(i);
                    model.fireTableDataChanged();

                }

                // DepartmentManagerPane.this.model.removeRow(0);
                i++;
            }
            resultTable.setModel(model);
            resultTable.revalidate();
            resultTable.repaint();

        } catch (SQLException e1) {
            // TODO Auto-generated catch block
            resultLabel.setText("Nie można usunąć wydania. Istnieją książki z nim skojarzone.");
            //e1.printStackTrace();
        }
    }//GEN-LAST:event_removeButtonActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        JDialog frame = new JDialog();
        currentDepartment = new Department(nameOfDepartment.getText(),
                idOfDepartment, caseNumberLabel.getText());
        DepartmentChoicePane edition = new DepartmentChoicePane(
                client, currentDepartment, this, frame);
        frame.add(edition);
        frame.setVisible(true);
        frame.setSize(edition.getPreferredSize());        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        JDialog frame = new JDialog();
        currentDepartment = new Department(nameOfDepartment.getText(),
                idOfDepartment, caseNumberLabel.getText());
        AuthorsPane edition = new AuthorsPane(
                client, this, frame, authors);
        frame.add(edition);
        frame.setVisible(true);
        frame.setSize(edition.getPreferredSize());        // TODO add your handling code here:
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed

        dateOfEditionTextField.setText("");// TODO add your handling code here:
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        jFormattedISBNField.setText("");// TODO add your handling code here:
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed

        this.caseNumberLabel.setText("Nie określono regału.");
        this.nameOfDepartment.setText("Nie wybrano działu.");
        this.currentDepartment = null;
        this.idOfDepartment = null;// TODO add your handling code here:
    }//GEN-LAST:event_jButton6ActionPerformed

    private void titleFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_titleFieldKeyTyped

        jButton2.setEnabled(false);        // TODO add your handling code here:
    }//GEN-LAST:event_titleFieldKeyTyped

    private void editionNameTextFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_editionNameTextFieldKeyTyped
        
        jButton2.setEnabled(false);        // TODO add your handling code here:
    }//GEN-LAST:event_editionNameTextFieldKeyTyped

    private void jFormattedISBNFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jFormattedISBNFieldKeyTyped

        jButton2.setEnabled(false);// TODO add your handling code here:
    }//GEN-LAST:event_jFormattedISBNFieldKeyTyped

    private void dateOfEditionTextFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_dateOfEditionTextFieldKeyTyped

        jButton2.setEnabled(false);// TODO add your handling code here:
    }//GEN-LAST:event_dateOfEditionTextFieldKeyTyped
    private LibrarianUserClient client = null;
    //data
    private String isbn = null;
    private String nameOfEditionHouse = "";
    private String dateOfPublishing;
    private String title = "";
    private String idOfDepartment = null;
    private DefaultTableModel model;
    private Integer idOfDepartmentTemp = null;
    private JDialog owner;
    private BooksManagementPane booksManagementsPane = null;
    private EditionBook currentEdition = null;
    private Department currentDepartment = null;
    private ArrayList<Author> authors = new ArrayList<Author>();
    private DefaultListModel authorsListModel = null;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addButton;
    private javax.swing.JList authorsList;
    private javax.swing.JLabel caseNumberLabel;
    private javax.swing.JFormattedTextField dateOfEditionTextField;
    private javax.swing.JTextField editionNameTextField;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JFormattedTextField jFormattedISBNField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel nameOfDepartment;
    private javax.swing.JButton removeButton;
    private javax.swing.JLabel resultLabel;
    private javax.swing.JTable resultTable;
    private javax.swing.JButton searchButton;
    private javax.swing.JTextField titleField;
    // End of variables declaration//GEN-END:variables
}
